package com.nnnu.wsnackshop.controller;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.controller.base.BaseController;
import com.nnnu.wsnackshop.pojo.dto.RiderLoginDTO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IRidersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 骑手认证控制器
 * </p>
 */
@RestController
@RequestMapping("/api/rider/auth")
@Tag(name = "骑手认证接口", description = "包含骑手登录等功能")
@Slf4j
@RequiredArgsConstructor
public class RiderAuthController extends BaseController {

    private final IRidersService ridersService;

    @Operation(summary = "骑手登录")
    @PostMapping("/login")
    public Result<?> login(@Valid @RequestBody RiderLoginDTO loginDTO) {
        log.info("骑手登录: {}", loginDTO.getPhone());
        try {
            String token = ridersService.login(loginDTO);

            Map<String, Object> result = new HashMap<>();
            result.put("token", token);

            return Result.success(result, MessageConstants.LOGIN_SUCCESS);
        } catch (Exception e) {
            log.error("骑手登录失败: {}", e.getMessage(), e);
            return Result.error(500, "登录失败: " + e.getMessage());
        }
    }
}